package com.tuanzi.loan.core.service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Sort;

import com.tuanzi.loan.core.entity.AuditEntity;
import com.tuanzi.loan.core.repository.JPAAuditRepository;

/**
 * @author Ray 2017/09/21
 */

public abstract class BaseAuditServiceImpl<T extends AuditEntity, PK extends Serializable>
        implements BaseAuditService<T, PK> {

    protected abstract JPAAuditRepository<T, PK> getRepository();

    @Override
    public void delete(final T t) {
        getRepository().delete(t);
    }

    @Override
    public List<T> findAll() {
        return getRepository().findAll();
    }

    @Override
    public List<T> findAll(Sort sort) {
        return getRepository().findAll(sort);
    }

    @Override
    public T findOne(final PK id) {
        return getRepository().findOne(id);
    }

    @Override
    public T saveOrUpdate(final T t) {
        return getRepository().save(t);
    }

    @Override
    public Boolean exists(PK id) {
        return getRepository().exists(id);
    }

    @Override
    public List<T> saveOrUpdate(List<T> t) {
        return t.parallelStream().map(this::saveOrUpdate).collect(Collectors.toList());
    }

    @Override
    public List<T> findAll(List<PK> ids) {
        return getRepository().findAll(ids);
    }

    @Override
    public void delete(List<T> entities) {
        getRepository().deleteInBatch(entities);
    }
}
